﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Telco.Core.Interfaces;
using Microsoft.Telco.Core.Interfaces.SMS;
using Microsoft.Telco.Web.Contracts;
using Microsoft.Telco.Web.Services.ServiceModel;

namespace Microsoft.Telco.Web.Services
{
    /// <summary>
    /// Service implementation of the <see cref="IGatewayService"/>.
    /// </summary>
    [ExceptionShielding]
    [ServiceBehavior(Namespace = Namespaces.V1, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class GatewayService : IGatewayService
    {
        ITelcoProvider telcoProvider;

        /// <summary>
        /// Initializes a new instance of the <see cref="GatewayService"/> class.
        /// </summary>
        public GatewayService()
        {
            telcoProvider = ServiceLocator.Current.GetInstance<ITelcoProvider>();
        }

        /// <summary>
        /// Sends an SMS message.
        /// </summary>
        /// <param name="userId">The id of the user sending the message</param>
        /// <param name="originAddress">The phone number the message comes from</param>
        /// <param name="destinationAdresses">Phone numbers to send SMS to.</param>
        /// <param name="message">The SMS message.</param>
        /// <returns>
        /// The message Id that can be used to query message delivery status
        /// </returns>
        public string SendSMS(string userId, string originAddress, string[] destinationAdresses, string message)
        {
            return telcoProvider.SendSMSMessage(userId, originAddress, destinationAdresses, message);
        }

        /// <summary>
        /// Gets the SMS status.
        /// </summary>
        /// <param name="userId">The id of the user sending the message</param>
        /// <param name="messageId">The message id.</param>
        /// <returns>
        /// The message delivery status
        /// </returns>
        public List<MessageStatus> GetSMSStatus(string userId, string messageId)
        {
            ISMSDeliveryStatus[] providerStatusCollection = telcoProvider.GetSMSStatus(userId, messageId);
            List<MessageStatus> returnStatusCollection = new List<MessageStatus>();
            foreach (var currentStatus in providerStatusCollection)
            {
                returnStatusCollection.Add(currentStatus as MessageStatus);
            }
            return returnStatusCollection;
        }

        /// <summary>
        /// Gets SMS messages for the application.
        /// </summary>
        /// <param name="userId">The id of the user sending the message</param>
        /// <returns></returns>
        public List<ShortMessage> GetSMSMessages(string userId)
        {
            ISMSMessage[] providerMessageCollection = telcoProvider.GetSMSMessages(userId);
            List<ShortMessage> returnMessageCollection = new List<ShortMessage>();
            foreach (var currentMessage in providerMessageCollection)
            {
                returnMessageCollection.Add(currentMessage as ShortMessage);
            }
            return returnMessageCollection;
        }

        /// <summary>
        /// Sends an MMS message.
        /// </summary>
        /// <param name="userId">The id of the user sending the message</param>
        /// <param name="originationAddress"></param>
        /// <param name="destinationAddresses">Phone numbers to send MMS to.</param>
        /// <param name="messageParts">A collection of attachments to be sent</param>
        /// <returns>
        /// The message Id that can be used to query message delivery status
        /// </returns>
        public string SendMMS(string userId, string originationAddress, List<string> destinationAddresses, List<MultimediaMessage> messageParts)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the MMS status.
        /// </summary>
        /// <param name="userId">The id of the user sending the message</param>
        /// <param name="messageId">The message id.</param>
        /// <returns>
        /// The message delivery status
        /// </returns>
        public List<MessageStatus> GetMMSStatus(string userId, string messageId)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets MMS messages for the application.
        /// </summary>
        /// <param name="userId">The id of the user sending the message</param>
        /// <returns>
        /// The collection of messages for the specified user
        /// </returns>
        public List<MultimediaMessage> GetMMSMessages(string userId)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the advertisement.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>
        /// The requested advertisement
        /// </returns>
        public Advertisement GetAdvertisement(AdvertisementRequest request)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the user's contacts.
        /// </summary>
        /// <returns></returns>
        public List<LiveContact> GetContacts()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Converts the text to speech.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <returns>
        /// Audio of the provided speech
        /// </returns>
        public Attachment ConvertTextToSpeech(string text)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Transcribes the voice.
        /// </summary>
        /// <param name="attachment">The attachment.</param>
        /// <returns>
        /// The transcribed speech
        /// </returns>
        public string TranscribeVoice(Attachment attachment)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Creates a call between the origin and destination
        /// </summary>
        /// <param name="userId">The id of the user sending the message</param>
        /// <param name="origin">The origin of the call request.</param>
        /// <param name="destination">The remote endpoint to be connected on this call.</param>
        /// <param name="connectionText">Text provided on connection</param>
        /// <returns>
        /// The status of the call
        /// </returns>
        public string CreateClickToCall(string userId, string origin, string destination, string connectionText)
        {
            return telcoProvider.CreateCall(userId, origin, destination, connectionText);
        }
    }
}
